---
title: "Political Exclusion and Support for Democratic Innovations: Evidence from a Conjoint Experiment on Participatory Budgeting"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# INSTALLING/LOADING REQUIRED PACKAGES

```{r}

#install.packages("cregg")
#install.packages("cjoint")
#install.packages("tidyverse")
#install.packages("ggpubr")
#install.packages("GGally")
#install.packages("grid")
#install.packages("ggthemes")
#install.packages("extrafont")
#install.packages("Rmisc")
#install.packages("doBy")
#install.packages("stringr")
#install.packages("plotly")
#if(!require(devtools)) install.packages("devtools")
#devtools::install_github("m-freitag/cjpowR")
# And install Rtools if requred, see: https://cran.r-project.org/bin/windows/Rtools/


library(cregg)
library(cjoint)
library(tidyverse)
library(ggpubr)
library(GGally)
library(grid)
library(ggthemes)
library(extrafont)
library(Rmisc)
library(doBy)
library(stringr)
library(devtools)
library(cjpowR)
library(plotly)

```

# LOAD THE DATA

```{r}

all_together <- read_csv(file="pb_data.csv")

all_together <- all_together %>% 
  mutate(profile=replace(profile, profile==1, "Left")) %>% 
  mutate(profile=replace(profile, profile==2, "Right"))

all_together$selected <- as.integer(all_together$selected)
all_together$residents_allowed_submit <- as.factor(all_together$residents_allowed_submit)
all_together$projects_allowed <- as.factor(all_together$projects_allowed)
all_together$support_municipality <- as.factor(all_together$support_municipality)
all_together$choice_winning_project <- as.factor(all_together$choice_winning_project)
all_together$outcome_favorability <- as.factor(all_together$outcome_favorability)
all_together$outcome_favorability_3_levels <- as.factor(all_together$outcome_favorability_3_levels)
all_together$profile <- as.factor(all_together$profile)
all_together$gender <- as.factor(all_together$gender)
all_together$profile <- as.factor(all_together$profile)

all_together <- all_together %>% mutate(sample_type=
                                 ifelse(small_cities==0, "10 largest municipalities",
                                 ifelse(small_cities==1, "Middle-sized and small municipalities",
                                 ifelse(small_cities==99, "1 of the 10 largest municipalities", NA))))

```

# DATA FOR TABLE 1

```{r}

overview_survey <- all_together %>% 
                   group_by(sample_type) %>% 
                   dplyr::summarise(count=n()) %>% 
                   mutate(sample_size=count/12)

overview_survey

```

# FIGURE 2

```{r, warning=FALSE, fig.width=10, fig.height=12}

all_together_pb_fav <- mutate(all_together, 
                        disadvataged = 
                          ifelse(higher_education=="Lower educated" & minority==1 & sex==2, 
                                        "Female + lower edu. + minority (N=100)", 
                          ifelse(higher_education=="Higher educated" & minority==2 & sex==1, 
                                        "Male + higher edu. + majority (N=599)", "")))

all_together_pb_fav <- mutate(all_together_pb_fav, 
                              gender = ifelse(sex==1, "Male (N=1,609)",
                                       ifelse(sex==2, "Female (N=1,618)", "")))
  
all_together_pb_fav <- mutate(all_together_pb_fav, 
                              minorities = ifelse(minority==1, "Minority (N=422)",
                                           ifelse(minority==2, "Majority (N=2,824)", "")))
  
all_together_pb_fav <- mutate(all_together_pb_fav, 
                              high_educ = ifelse(higher_education=="Higher educated", 
                                                 "Higher edu. (N=1,377)",
                                          ifelse(higher_education=="Lower educated", 
                                                 "Lower edu. (N=1,790)", "")))

collapse_pb_fav <- summaryBy(selected ~ responseid, FUN = c(mean), data = all_together_pb_fav)

data <- all_together_pb_fav %>% distinct(responseid, .keep_all = TRUE)
data <- data %>% dplyr::select(-selected)

merged_data <- merge(collapse_pb_fav, data, by = 'responseid')

intersectionality <- merged_data %>% filter(disadvataged=="Female + lower edu. + minority (N=100)" | 
                               disadvataged=="Male + higher edu. + majority (N=599)")
intersectionality$disadvataged <- as.factor(intersectionality$disadvataged) 
intersectionality <- summarySE(intersectionality, measurevar = "selected.mean", groupvars=c("disadvataged"))
intersectionality <- intersectionality %>% dplyr::rename(group = disadvataged)
  
gender <- merged_data %>% filter(gender=="Male (N=1,609)" | gender=="Female (N=1,618)")
gender$gender <- as.factor(gender$gender)
gender <- summarySE(gender, measurevar = "selected.mean", groupvars = c("gender"))
gender <- gender %>% dplyr::rename(group = gender)
  
minority <- merged_data %>% filter(minorities=="Minority (N=422)" | minorities=="Majority (N=2,824)")
minority$minorities <- as.factor(minority$minorities)  
minority <- summarySE(minority, measurevar = "selected.mean", groupvars = c("minorities"))
minority <- minority %>% dplyr::rename(group=minorities)
  
education <- merged_data %>% filter(high_educ=="Lower edu. (N=1,790)" | 
                                    high_educ=="Higher edu. (N=1,377)")
education$high_educ <- as.factor(education$high_educ)
education <- summarySE(education, measurevar = "selected.mean", groupvars = c("high_educ"))
education <- education %>% dplyr::rename(group=high_educ)
  
all <- rbind(gender, education, minority, intersectionality)
  
all$group2 <- factor(all$group, levels= c("Female + lower edu. + minority (N=100)",
                                               "Male + higher edu. + majority (N=599)",
                                               "Minority (N=422)", 
                                               "Majority (N=2,824)",
                                               "Lower edu. (N=1,790)",
                                               "Higher edu. (N=1,377)",
                                               "Female (N=1,618)",
                                               "Male (N=1,609)"))
  
all$group2 <- as.factor(all$group2)
  
all_figure <- all %>% mutate(data="PB favourability")

figure_2 <- ggplot(all_figure[all_figure$data=="PB favourability",], 
                    aes(x=group2, y=selected.mean)) + 
             geom_errorbar(aes(ymin=selected.mean-se, ymax=selected.mean+se), width=.1) +
             geom_point() + 
             coord_flip() +
             xlab("") +
             ylab("") +
             ggtitle("") +
             theme_wsj(base_size = 12, 
                       color = "gray", 
                       base_family = "Times New Roman",
                       title_family = "Times New Roman") + 
             scale_colour_wsj("colors6") +
             ylim(4.5, 5) +
             geom_text(all_figure[all_figure$data=="PB favourability",], 
                       mapping=aes(label=round(selected.mean,2)), 
                       parse = TRUE, 
                       color="black", 
                       size=5, 
                       check_overlap = TRUE, 
                       vjust = 1.8) +
             theme(text = element_text(family="Times New Roman", size=20),
                   plot.title=element_text(size=25,face="bold"),
                   legend.text=element_text(size=20,face="bold"),
                   strip.text.x = element_text(size = 20)) +
             theme(legend.position="bottom") +
             scale_color_manual(values=c('maroon','black')) +
             theme(legend.title = element_blank())
  
plot(figure_2)

ggsave(figure_2, 
       file="figure_2.png",
       width = 12, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```

# FIGURE 3

## Male vs. Female

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_gender <- all_together %>% filter(gender=="Male" | gender=="Female") 
all_together_gender$gender <- as.factor(all_together_gender$gender)
all_together_gender$gender <- factor(all_together_gender$gender, levels= c("Male", "Female"))
all_together_gender %>% dplyr::group_by(gender) %>% dplyr::summarise(mean_outcome=mean(selected))

fc_differences_gender <- cj(all_together_gender, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~gender,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_gender_plot <- plot(fc_differences_gender, 
                                    vline = 0.5, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")


fc_differences_gender_plot_final <- fc_differences_gender_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.35, 0.65)) +
                                   labs(x = "",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "maroon", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Gender") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())

```

## Minority vs Majority

```{r, warning=FALSE, fig.width=10, fig.height=8}
  
all_together$minorities <- as.factor(all_together$minorities)
   
fc_differences_minority <- cj(all_together, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability  +
                                profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~minorities,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_minority_plot <- plot(fc_differences_minority, 
                                      vline = 0.5, 
                                      size = 2, 
                                      feature_headers = FALSE, 
                                      color = "black")


fc_differences_minority_plot_final <- fc_differences_minority_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.35, 0.65)) +
                                   labs(x = "",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "maroon", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Minority") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())

```

## Intersectionality

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_disadv <- all_together %>% mutate(disadvataged =
                  ifelse(higher_education == "Lower educated" & minority == 1 & sex == 2, 
                                        "Excluded", 
                  ifelse(higher_education == "Higher educated" & minority == 2 & sex == 1, 
                                        "Not excluded", "")))
  
all_together_disadv <- all_together_disadv %>% filter(disadvataged=="Excluded" | disadvataged=="Not excluded") 
all_together_disadv$disadvataged <- as.factor(all_together_disadv$disadvataged)
all_together_disadv$disadvataged <- factor(all_together_disadv$disadvataged, 
                                           levels= c("Not excluded",
                                                     "Excluded"))
   
fc_differences_disadv <- cj(all_together_disadv, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                  profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~disadvataged,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_disadv_plot <- plot(fc_differences_disadv, 
                                             vline = 0.5, 
                                             size = 2, 
                                             feature_headers = FALSE, 
                                             color = "black")
        
fc_differences_disadv_plot_final <- fc_differences_disadv_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size = 15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.35, 0.65)) +
                                   labs(x = "",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "maroon", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Intersectionality") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())
     
```

## Lower vs Higher Educated

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_higher_education <- all_together %>% 
    filter(higher_education=="Higher educated" | higher_education=="Lower educated") 
all_together_higher_education$higher_education <- as.factor(all_together_higher_education$higher_education)
   
fc_differences_education <- cj(all_together_higher_education, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~higher_education,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project ="Who is choosing the winning project?",
                                         profile = "Profile display"))
    
fc_differences_education_plot <- plot(fc_differences_education, 
                                     vline = 0.5, 
                                     size = 2, 
                                     feature_headers = FALSE, 
                                     color = "black")

fc_differences_education_plot_final <- fc_differences_education_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.35, 0.65)) +
                                   labs(x = "",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "maroon", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                  ggtitle("Education") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())

```

## Combine all figures into one

```{r, warning=FALSE, fig.width=15, fig.height=20}
figure_3 <- ggarrange(fc_differences_gender_plot_final, 
                      fc_differences_education_plot_final,
                      fc_differences_minority_plot_final,
                      fc_differences_disadv_plot_final,
                      ncol = 2, nrow = 2, 
                      widths = c(3.5, 2))

plot(figure_3)

ggsave("figure_3.png",
       width = 15, 
       height = 20, 
       dpi = 150, 
       units = "in", 
       device='png',
       scale = 0.85)

```

# APPENDIX

## FIGURE A5
## MALE VS FEMALE

```{r, warning=FALSE}

all_together_gender <- all_together %>% filter(gender=="Male" | gender=="Female") 
all_together_gender$gender <- as.factor(all_together_gender$gender)
all_together_gender$gender <- factor(all_together_gender$gender, levels= c("Male", "Female"))
all_together_gender %>% dplyr::group_by(gender) %>% dplyr::summarise(mean_outcome=mean(selected))

males <- all_together_gender %>% filter(gender=="Male")

f_m <- cj_freqs(males, ~  residents_allowed_submit +
                 projects_allowed +
                 support_municipality + 
                 choice_winning_project +
                 outcome_favorability, 
               id = ~ responseid)

f_m <- f_m %>% mutate(gender="Male") %>% mutate(estimate=estimate/19038*100)

females <- all_together_gender %>% filter(gender=="Female") 

f_f <- cj_freqs(females, ~  residents_allowed_submit +
                   projects_allowed +
                   support_municipality + 
                   choice_winning_project +
                   outcome_favorability, 
                 id = ~ responseid)

f_f <- f_f %>% mutate(gender="Female") %>% mutate(estimate=estimate/19416*100)

f <- bind_rows(f_m, f_f)

p <- ggplot(f, aes(x = level, 
                               y = estimate, 
                               fill = gender))


p + geom_col(position = "dodge2") +
  coord_flip() +
  theme_bw() +
  labs(x = "Levels",y = "Percent") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values = c("maroon", "black")) +
  theme(legend.position="right")

ggsave("figure_a5_part1.png",
       width = 10, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png',
       scale=0.8)

```


## FIGURE A5 CONT
## MINORITY VS MAJORITY

```{r, warning=FALSE}

all_together$minorities <- as.factor(all_together$minorities)

table(all_together$minorities)

majority_minority <- all_together
majority <- majority_minority %>% filter(minorities=="Majority")

f_maj <- cj_freqs(majority, ~  residents_allowed_submit +
                   projects_allowed +
                   support_municipality + 
                   choice_winning_project +
                   outcome_favorability, 
                 id = ~ responseid)

f_maj <- f_maj %>% mutate(minorities="Majority")  %>% mutate(estimate=estimate/33888*100)

minority <- majority_minority %>% filter(minorities=="Minority") 

f_min <- cj_freqs(minority, ~  residents_allowed_submit +
                   projects_allowed +
                   support_municipality + 
                   choice_winning_project +
                   outcome_favorability, 
                 id = ~ responseid)

f_min <- f_min %>% mutate(minorities="Minority")  %>% mutate(estimate=estimate/5064*100)

f <- bind_rows(f_maj, f_min)

p <- ggplot(f, aes(x = level, 
                   y = estimate, 
                   fill = minorities))

p + geom_col(position = "dodge2") +
  coord_flip() +
  theme_bw() +
  labs(x = "Levels",y = "Percent") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values = c("maroon", "black")) +
  theme(legend.position="right")

ggsave("figure_a5_part2.png",
       width = 10, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png',
       scale=0.8)

```

## FIGURE A5 CONT
## HIGHER VS LOWER EDUCATED

```{r, warning=FALSE}

all_together_higher_education <- all_together %>% 
  filter(higher_education=="Higher educated" | higher_education=="Lower educated") 
all_together_higher_education$higher_education <- as.factor(all_together_higher_education$higher_education)

table(all_together_higher_education$higher_education)

high <- all_together_higher_education %>% filter(higher_education=="Higher educated")

f_high <- cj_freqs(high, ~  residents_allowed_submit +
                     projects_allowed +
                     support_municipality + 
                     choice_winning_project +
                     outcome_favorability, 
                   id = ~ responseid)

f_high <- f_high %>% mutate(higher_education="Higher educated")  %>% mutate(estimate=estimate/16524*100)

f_high

low <- all_together_higher_education %>% filter(higher_education=="Lower educated") 

f_low <- cj_freqs(low, ~  residents_allowed_submit +
                     projects_allowed +
                     support_municipality + 
                     choice_winning_project +
                     outcome_favorability, 
                   id = ~ responseid)

f_low <- f_low %>% mutate(higher_education="Lower educated")  %>% mutate(estimate=estimate/21480*100)

f_low

f <- bind_rows(f_high, f_low)

p <- ggplot(f, aes(x = level, 
                   y = estimate, 
                   fill = higher_education))


p + geom_col(position = "dodge2") +
  coord_flip() +
  theme_bw() +
  labs(x = "Levels",y = "Percent") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values = c("maroon", "black")) +
  theme(legend.position="right")

ggsave("figure_a5_part3.png",
       width = 10, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png',
       scale=0.8)

```

## FIGURE A6

```{r, warning=FALSE}

all_together$task <- as.factor(all_together$task)

plot(cj(all_together, selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
        id = ~responseid, 
        by = ~task, 
        estimate = "mm"), 
     group = "task", 
     vline = 0.5)

ggsave("figure_a6.png",
       width = 8, 
       height = 8, 
       dpi = 150, 
       units = "in", 
       device='png')


```

## FIGURE A7

```{r, warning=FALSE}

plot(cj(all_together, selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
        id = ~responseid, 
        by = ~profile, 
        estimate = "mm"), 
     group = "profile", 
     vline = 0.5)

ggsave("figure_a7.png",
       width = 8, 
       height = 8, 
       dpi = 150, 
       units = "in", 
       device='png')

```

## FIGURE A8

```{r, warning=FALSE}

all_together$task <- as.factor(all_together$task)

plot(cj(all_together, choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
        id = ~responseid, 
        by = ~task, 
        estimate = "mm"), 
     group = "task", 
     vline = 0.5)

ggsave("figure_a8.png",
       width = 8, 
       height = 8, 
       dpi = 150, 
       units = "in", 
       device='png')


```

## FIGURE A9

```{r, warning=FALSE}

plot(cj(all_together, choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
        id = ~responseid, 
        by = ~profile, 
        estimate = "mm"), 
     group = "profile", 
     vline = 0.5)

ggsave("figure_a9.png",
       width = 8, 
       height = 8, 
       dpi = 150, 
       units = "in", 
       device='png')

```


## FIGURE A10

## Male vs. Female

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_gender <- all_together %>% filter(gender=="Male" | gender=="Female") 
all_together_gender$gender <- as.factor(all_together_gender$gender)
all_together_gender$gender <- factor(all_together_gender$gender, levels= c("Male", "Female"))
all_together_gender %>% dplyr::group_by(gender) %>% dplyr::summarise(mean_outcome=mean(selected))

rat_differences_gender <- mm_diffs(all_together_gender, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                h0 = 0.120596,
                                by = ~gender,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

rat_differences_gender_plot <- plot(rat_differences_gender, 
                                    vline = 0.120596, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")


rat_differences_gender_plot_final <- rat_differences_gender_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   geom_text(rat_differences_gender, 
                                             mapping = aes(label = round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.4)) +
                                   labs(x = "Female - male",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Gender") 

        

```

## Minority vs Majority

```{r, warning=FALSE, fig.width=10, fig.height=8}
  
all_together$minorities <- as.factor(all_together$minorities)
   
rat_differences_minority <- mm_diffs(all_together, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability  +
                                profile, 
                                id = ~responseid,
                                h0 = 0.145155,
                                by = ~minorities,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

rat_differences_minority_plot <- plot(rat_differences_minority, 
                                      vline = 0.145155, 
                                      size = 2, 
                                      feature_headers = FALSE, 
                                      color = "black")


rat_differences_minority_plot_final <- rat_differences_minority_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(rat_differences_minority, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color = "black", 
                                             size = 3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.4)) +
                                   labs(x = "Minority - majority",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Minority")
        
```

## Intersectionality

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_disadv <- all_together %>% mutate(disadvataged =
                  ifelse(higher_education == "Lower educated" & minority == 1 & sex == 2, 
                                        "Excluded", 
                  ifelse(higher_education == "Higher educated" & minority == 2 & sex == 1, 
                                        "Not excluded", "")))
  
all_together_disadv <- all_together_disadv %>% filter(disadvataged=="Excluded" | disadvataged=="Not excluded") 
all_together_disadv$disadvataged <- as.factor(all_together_disadv$disadvataged)
all_together_disadv$disadvataged <- factor(all_together_disadv$disadvataged, 
                                           levels= c("Not excluded",
                                                     "Excluded"))
   
rat_differences_disadv <- mm_diffs(all_together_disadv, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                  profile, 
                                id = ~responseid,
                                h0 = 0.277132,
                                by = ~disadvataged,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

        
    
rat_differences_disadv_plot <- plot(rat_differences_disadv, 
                                             vline = 0.277132, 
                                             size = 2, 
                                             feature_headers = FALSE, 
                                             color = "black")
        
rat_differences_disadv_plot_final <- rat_differences_disadv_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position="none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size = 15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(rat_differences_disadv, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.65)) +
                                   labs(x = "Excluded - not excluded",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Intersectionality")


     
```

## Lower vs Higher Educated

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_higher_education <- all_together %>% 
    filter(higher_education=="Higher educated" | higher_education=="Lower educated") 
all_together_higher_education$higher_education <- as.factor(all_together_higher_education$higher_education)
   
rat_differences_education <- mm_diffs(all_together_higher_education, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                profile, 
                                id = ~responseid,
                                h0 = 0.047791,
                                by = ~higher_education,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project ="Who is choosing the winning project?",
                                         profile = "Profile display"))

        
    
rat_differences_education_plot <- plot(rat_differences_education, 
                                     vline = 0.047791, 
                                     size = 2, 
                                     feature_headers = FALSE, 
                                     color = "black")


rat_differences_education_plot_final <- rat_differences_education_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(rat_differences_education, 
                                             mapping=aes(label=round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.4)) +
                                   labs(x = "Lower - higher educated",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                  ggtitle("Education")

     
```

## Combine all figures into one

```{r, warning=FALSE, fig.width=20, fig.height=10}
figure_a10 <- ggarrange(rat_differences_gender_plot_final, 
                      rat_differences_education_plot_final,
                      rat_differences_minority_plot_final,
                      rat_differences_disadv_plot_final,
                      ncol = 4, 
                      widths = c(3.1, 1.5, 1.5, 1.5))

plot(figure_a10)

ggsave("figure_a10.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```

## FIGURE A11

## Male vs. Female

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_gender <- all_together %>% filter(gender=="Male" | gender=="Female") 
all_together_gender$gender <- as.factor(all_together_gender$gender)
all_together_gender$gender <- factor(all_together_gender$gender, levels= c("Male", "Female"))
all_together_gender %>% dplyr::group_by(gender) %>% dplyr::summarise(mean_outcome=mean(selected))

fc_differences_gender <- mm_diffs(all_together_gender, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability_3_levels+
                                profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~gender,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability_3_levels",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability_3_levels = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_gender_plot <- plot(fc_differences_gender, 
                                    vline = 0, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")


fc_differences_gender_plot_final <- fc_differences_gender_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_gender, 
                                             mapping = aes(label = round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Female - male",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Gender") 

```

## Minority vs Majority

```{r, warning=FALSE, fig.width=10, fig.height=8}
  
all_together$minorities <- as.factor(all_together$minorities)
   
fc_differences_minority <- mm_diffs(all_together, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability_3_levels  +
                                profile, 
                                id = ~responseid,
                                h0 = 0.0,
                                by = ~minorities,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability_3_levels",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability_3_levels = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_minority_plot <- plot(fc_differences_minority, 
                                      vline = 0, 
                                      size = 2, 
                                      feature_headers = FALSE, 
                                      color = "black")


fc_differences_minority_plot_final <- fc_differences_minority_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_minority, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color = "black", 
                                             size = 3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Minority - majority",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Minority")

```

## Intersectionality

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_disadv <- all_together %>% mutate(disadvataged =
                  ifelse(higher_education == "Lower educated" & minority == 1 & sex == 2, 
                                        "Excluded", 
                  ifelse(higher_education == "Higher educated" & minority == 2 & sex == 1, 
                                        "Not excluded", "")))
  
all_together_disadv <- all_together_disadv %>% filter(disadvataged=="Excluded" | disadvataged=="Not excluded") 
all_together_disadv$disadvataged <- as.factor(all_together_disadv$disadvataged)
all_together_disadv$disadvataged <- factor(all_together_disadv$disadvataged, 
                                           levels= c("Not excluded",
                                                     "Excluded"))
   
fc_differences_disadv <- mm_diffs(all_together_disadv, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability_3_levels +
                                  profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~disadvataged,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability_3_levels",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability_3_levels = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

        
    
fc_differences_disadv_plot <- plot(fc_differences_disadv, 
                                             vline = 0, 
                                             size = 2, 
                                             feature_headers = FALSE, 
                                             color = "black")
        
fc_differences_disadv_plot_final <- fc_differences_disadv_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position="none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size = 15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_disadv, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Excluded - not excluded",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Intersectionality")
     
```

## Lower vs Higher Educated

```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_higher_education <- all_together %>% 
    filter(higher_education=="Higher educated" | higher_education=="Lower educated") 
all_together_higher_education$higher_education <- as.factor(all_together_higher_education$higher_education)
   
fc_differences_education <- mm_diffs(all_together_higher_education, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability_3_levels +
                                profile, 
                                id = ~responseid,
                                h0 = 0.0,
                                by = ~higher_education,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability_3_levels",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability_3_levels = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project ="Who is choosing the winning project?",
                                         profile = "Profile display"))

        
    
fc_differences_education_plot <- plot(fc_differences_education, 
                                     vline = 0.0, 
                                     size = 2, 
                                     feature_headers = FALSE, 
                                     color = "black")


fc_differences_education_plot_final <- fc_differences_education_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_education, 
                                             mapping=aes(label=round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Lower - higher educated",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                  ggtitle("Education")
     
```

## Combine all figures into one

```{r, warning=FALSE, warning=FALSE, fig.width=20, fig.height=10}
figure_a11 <- ggarrange(fc_differences_gender_plot_final, 
                      fc_differences_education_plot_final,
                      fc_differences_minority_plot_final,
                      fc_differences_disadv_plot_final,
                      ncol = 4, 
                      widths = c(3.1, 1.5, 1.5, 1.5))

plot(figure_a11)

ggsave("figure_a11.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```

## FIGURE A12

```{r, warning=FALSE}

# Install Rtools if required, see: https://cran.r-project.org/bin/windows/Rtools/”

cjpowr_amce(amce = 0.05, power = 0.8, levels = 4, alpha=0.05)


cjpowr_plotly(y = "power", x = "n", by = "amce",
              amce = c(0.03, 0.04, 0.05),
              n = seq(from = 1200, to = 34000, by = 500),
              alpha = 0.05,
              levels = 4,
              delta0 = 0.5)

# Save this figure manually

```

## FIGURE A13

```{r, warning=FALSE, fig.width=20, fig.height=10}

all_together_pb_fav <- mutate(all_together, 
                        disadvataged = 
                          ifelse(higher_education=="Lower educated" & minority==1 & sex==2, 
                                        "Female + lower edu. + minority (N=100)", 
                          ifelse(higher_education=="Higher educated" & minority==2 & sex==1, 
                                        "Male + higher edu. + majority (N=599)", "")))

all_together_pb_fav <- mutate(all_together_pb_fav, 
                              gender = ifelse(sex==1, "Male (N=1,609)",
                                       ifelse(sex==2, "Female (N=1,618)", "")))
  
all_together_pb_fav <- mutate(all_together_pb_fav, 
                              minorities = ifelse(minority==1, "Minority (N=422)",
                                           ifelse(minority==2, "Majority (N=2,824)", "")))
  
all_together_pb_fav <- mutate(all_together_pb_fav, 
                              high_educ = ifelse(higher_education=="Higher educated", "Higher edu. (N=1,377)",
                                          ifelse(higher_education=="Lower educated", "Lower edu. (N=1,790)", "")))

collapse_pb_fav <- summaryBy(selected ~ responseid, FUN = c(mean), data = all_together_pb_fav)

collapse_efficacy <- summaryBy(external_efficacy ~ responseid, FUN = c(mean), data = all_together_pb_fav)

collapse_relational <- summaryBy(relational_trust ~ responseid, FUN = c(mean), data = all_together_pb_fav)

data <- all_together_pb_fav %>% distinct(responseid, .keep_all = TRUE)
data <- data %>% dplyr::select(-selected)

merged_data <- merge(collapse_pb_fav, data, by = 'responseid')
merged_data <- merge(merged_data, collapse_efficacy, by = 'responseid')
merged_data <- merge(merged_data, collapse_relational, by = 'responseid')
      
intersectionality_eff <- merged_data %>% filter(disadvataged=="Female + lower edu. + minority (N=100)" |
                                                disadvataged=="Male + higher edu. + majority (N=599)")
intersectionality_eff$disadvataged <- as.factor(intersectionality_eff$disadvataged) 
intersectionality_eff <- summarySE(intersectionality_eff, 
                                   measurevar = "external_efficacy.mean", 
                                   groupvars = c("disadvataged"))
intersectionality_eff <- intersectionality_eff %>% dplyr::rename(group = disadvataged)

gender_eff <- merged_data %>% filter(gender=="Male (N=1,609)" | gender=="Female (N=1,618)")
gender_eff$gender <- as.factor(gender_eff$gender)
gender_eff <- summarySE(gender_eff, measurevar = "external_efficacy.mean", groupvars = c("gender"))
gender_eff <- gender_eff %>% dplyr::rename(group = gender)

minority_eff <- merged_data %>% filter(minorities=="Minority (N=422)" | minorities=="Majority (N=2,824)")
minority_eff$minorities <- as.factor(minority_eff$minorities)  
minority_eff <- summarySE(minority_eff, measurevar="external_efficacy.mean", groupvars=c("minorities"))
minority_eff <- minority_eff %>% dplyr::rename(group=minorities)
  
  
education_eff <- merged_data %>% filter(high_educ=="Lower edu. (N=1,790)" | 
                                        high_educ=="Higher edu. (N=1,377)" )
education_eff$high_educ <- as.factor(education_eff$high_educ)
education_eff <- summarySE(education_eff, measurevar="external_efficacy.mean", groupvars = c("high_educ"))
education_eff <- education_eff %>% dplyr::rename(group=high_educ)
  
all_eff <- rbind(gender_eff, education_eff, minority_eff, intersectionality_eff)
  
all_eff$group2 <- factor(all_eff$group, levels= c(
                                                    "Female + lower edu. + minority (N=100)",
                                                    "Male + higher edu. + majority (N=599)",
                                                    "Minority (N=422)", 
                                                    "Majority (N=2,824)",
                                                    "Lower edu. (N=1,790)",
                                                    "Higher edu. (N=1,377)",
                                                    "Female (N=1,618)",
                                                    "Male (N=1,609)"))
  
all_eff$group2 <- as.factor(all_eff$group2)

all_eff <- all_eff %>% mutate(data="External efficacy") %>% 
                       dplyr::rename(selected.mean = external_efficacy.mean)


intersectionality_relational <- merged_data %>% filter(disadvataged=="Female + lower edu. + minority (N=100)" |
                                                disadvataged=="Male + higher edu. + majority (N=599)")
intersectionality_relational$disadvataged <- as.factor(intersectionality_relational$disadvataged) 
intersectionality_relational <- summarySE(intersectionality_relational, 
                                   measurevar = "relational_trust.mean", 
                                   groupvars = c("disadvataged"))
intersectionality_relational <- intersectionality_relational %>% dplyr::rename(group = disadvataged)

gender_relational <- merged_data %>% filter(gender=="Male (N=1,609)" | gender=="Female (N=1,618)")
gender_relational$gender <- as.factor(gender_relational$gender)
gender_relational <- summarySE(gender_relational, measurevar = "relational_trust.mean", groupvars = c("gender"))
gender_relational <- gender_relational %>% dplyr::rename(group = gender)

minority_relational <- merged_data %>% filter(minorities=="Minority (N=422)" | minorities=="Majority (N=2,824)")
minority_relational$minorities <- as.factor(minority_relational$minorities)  
minority_relational <- summarySE(minority_relational, measurevar="relational_trust.mean", groupvars=c("minorities"))
minority_relational <- minority_relational %>% dplyr::rename(group=minorities)
  
  
education_relational <- merged_data %>% filter(high_educ=="Lower edu. (N=1,790)" | 
                                        high_educ=="Higher edu. (N=1,377)" )
education_relational$high_educ <- as.factor(education_relational$high_educ)
education_relational <- summarySE(education_relational, measurevar="relational_trust.mean", groupvars = c("high_educ"))
education_relational <- education_relational %>% dplyr::rename(group=high_educ)
  
all_relational <- rbind(gender_relational, education_relational, minority_relational, intersectionality_relational)
  
all_relational$group2 <- factor(all_relational$group, levels= c(
                                                    "Female + lower edu. + minority (N=100)",
                                                    "Male + higher edu. + majority (N=599)",
                                                    "Minority (N=422)", 
                                                    "Majority (N=2,824)",
                                                    "Lower edu. (N=1,790)",
                                                    "Higher edu. (N=1,377)",
                                                    "Female (N=1,618)",
                                                    "Male (N=1,609)"))
  
all_relational$group2 <- as.factor(all_relational$group2)

all_relational <- all_relational %>% mutate(data="Relational trust") %>% 
                       dplyr::rename(selected.mean = relational_trust.mean)

all_figure <- rbind(all_relational, all_eff)

figure_relational <- ggplot(all_figure[all_figure$data=="Relational trust",], 
                    aes(x=group2, y=selected.mean)) + 
             geom_errorbar(aes(ymin=selected.mean-se, ymax=selected.mean+se), width=.1) +
             geom_point() + 
             coord_flip() +
             xlab("") +
             ylab("") +
             ggtitle("Local politicians \nshare my norms and values") +
             theme_wsj(base_size = 12, 
                       color = "gray", 
                       base_family = "Times New Roman",
                       title_family = "Times New Roman") + 
             scale_colour_wsj("colors6") +
             ylim(3.5, 4.5) +
             geom_text(all_figure[all_figure$data=="Relational trust",], 
                       mapping=aes(label=round(selected.mean,2)), 
                       parse = TRUE, 
                       color="black", 
                       size=5, 
                       check_overlap = TRUE, 
                       vjust = 1.8) +
             theme(text = element_text(family="Times New Roman", size=20),
                   plot.title=element_text(size=25,face="bold",hjust = 0.5),
                   legend.text=element_text(size=20,face="bold"),
                   strip.text.x = element_text(size = 20)) +
             theme(legend.position="bottom") +
             scale_color_manual(values=c('maroon','black')) +
             theme(legend.title = element_blank())
  

figure_eff <- ggplot(all_figure[all_figure$data=="External efficacy",], aes(x=group2, y=selected.mean)) + 
             geom_errorbar(aes(ymin=selected.mean-se, ymax=selected.mean+se), width=.1) +
             geom_point() + 
             coord_flip() +
             xlab("") +
             ylab("") +
             ggtitle("An ordinary citizen like me \ncannot influence local politics") +
             theme_wsj(base_size = 12, 
                       color = "gray", 
                       base_family = "Times New Roman",
                       title_family = "Times New Roman") + 
             scale_colour_wsj("colors6") +
             ylim(3.75, 4.8) +
             geom_text(all_figure[all_figure$data=="External efficacy",], 
                       mapping=aes(label=round(selected.mean,2)), 
                       parse = TRUE, 
                       color="black", 
                       size=5, 
                       check_overlap = TRUE, 
                       vjust = 1.8) +
             theme(text = element_text(family = "Times New Roman", size = 20),
                   plot.title = element_text(size = 25, face = "bold",hjust = 0.5),
                   legend.text = element_text(size=20,face = "bold"),
                   strip.text.x = element_text(size = 20)) +
             theme(legend.position = "bottom") +
             scale_color_manual(values = c('maroon','black')) +
             theme(legend.title = element_blank())        
 

figure_a13 <- ggarrange(figure_relational, figure_eff, ncol=2)

plot(figure_a13)

ggsave(figure_a13, 
       file="figure_a13.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')


```

## FIGURE A14

### Lack of voice
```{r, warning=FALSE}

all_together_lack_voice <- all_together %>% mutate(lack_voice_dummy=
                                            ifelse(relational_trust<4, "Strong", 
                                            ifelse(relational_trust>=4, "Weak", NA))) %>% drop_na(lack_voice_dummy) 

all_together_lack_voice$lack_voice_dummy <- as.factor(all_together_lack_voice$lack_voice_dummy)


fc_differences_lack_voice <- cj(all_together_lack_voice, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~lack_voice_dummy,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_lack_voice_plot <- plot(fc_differences_lack_voice, 
                                    vline = 0.5, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")

fc_differences_lack_voice_plot_final <- fc_differences_lack_voice_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.3, 0.7)) +
                                   labs(x = "Weak - strong",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "red", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Lack of voice") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())

```

### Lack of influence
```{r, warning=FALSE}

all_together_lack_influence <- all_together %>% mutate(lack_influence_dummy=
                                                ifelse(external_efficacy>4, "Strong", 
                                                ifelse(external_efficacy<=4, "Weak", NA))) %>% drop_na(lack_influence_dummy) 

all_together_lack_influence$lack_influence_dummy <- as.factor(all_together_lack_influence$lack_influence_dummy)

table(all_together_lack_influence$lack_influence_dummy)

fc_differences_lack_influence <- cj(all_together_lack_influence, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                estimate = "mm",
                                by = ~lack_influence_dummy,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_lack_influence_plot <- plot(fc_differences_lack_influence, 
                                    vline = 0.5, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")


fc_differences_lack_influence_plot_final <- fc_differences_lack_influence_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   scale_x_continuous(limits = c(0.3, 0.7)) +
                                   labs(x = "Weak - strong",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "red", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Lack of influence") +
                                  theme(legend.position="bottom") +
                                  theme(legend.title = element_blank())

```

### Combine the figures
```{r, warning=FALSE, fig.width=20, fig.height=10}

figure_a14 <- ggarrange(fc_differences_lack_voice_plot_final,
                      fc_differences_lack_influence_plot_final,
                      ncol = 2, 
                      widths = c(3, 2))

plot(figure_a14)

ggsave("figure_a14.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```

## FIGURE A15

### Lack of voice
```{r, warning=FALSE}

all_together_lack_voice <- all_together %>% mutate(lack_voice_dummy=
                                            ifelse(relational_trust<4, "Strong", 
                                            ifelse(relational_trust>=4, "Weak", NA))) %>% drop_na(lack_voice_dummy) 

all_together_lack_voice$lack_voice_dummy <- as.factor(all_together_lack_voice$lack_voice_dummy)

fc_differences_lack_voice <- mm_diffs(all_together_lack_voice, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~lack_voice_dummy,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_lack_voice_plot <- plot(fc_differences_lack_voice, 
                                    vline = 0, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")

fc_differences_lack_voice_plot_final <- fc_differences_lack_voice_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_lack_voice, 
                                             mapping = aes(label = round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Weak - strong",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Lack of voice")

```

### Lack of influence
```{r, warning=FALSE}

all_together_lack_influence <- all_together %>% mutate(lack_influence_dummy=
                                                ifelse(external_efficacy>4, "Strong", 
                                                ifelse(external_efficacy<=4, "Weak", NA))) %>% drop_na(lack_influence_dummy) 

all_together_lack_influence$lack_influence_dummy <- as.factor(all_together_lack_influence$lack_influence_dummy)

table(all_together_lack_influence$lack_influence_dummy)

fc_differences_lack_influence <- mm_diffs(all_together_lack_influence, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~lack_influence_dummy,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_lack_influence_plot <- plot(fc_differences_lack_influence, 
                                    vline = 0, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")

fc_differences_lack_influence_plot_final <- fc_differences_lack_influence_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_lack_influence, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color = "black", 
                                             size = 3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Weak - strong",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Lack of influence")

```

### Combine the figures
```{r, warning=FALSE, fig.width=20, fig.height=10}

figure_a15 <- ggarrange(fc_differences_lack_voice_plot_final,
                      fc_differences_lack_influence_plot_final,
                      ncol = 2, 
                      widths = c(3, 2))

plot(figure_a15)

ggsave("figure_a15.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```

## FIGURE A16

## Male vs. Female
```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_gender <- all_together %>% filter(gender=="Male" | gender=="Female") 
all_together_gender$gender <- as.factor(all_together_gender$gender)
all_together_gender$gender <- factor(all_together_gender$gender, levels= c("Male", "Female"))
all_together_gender %>% dplyr::group_by(gender) %>% dplyr::summarise(mean_outcome=mean(selected))

fc_differences_gender <- mm_diffs(all_together_gender, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability+
                                profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~gender,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_gender_plot <- plot(fc_differences_gender, 
                                    vline = 0, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")

fc_differences_gender_plot_final <- fc_differences_gender_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol = 1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_gender, 
                                             mapping = aes(label = round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Female - male",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                   scale_color_manual(values = c("black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black", 
                                                                 "black")) +
                                  ggtitle("Gender")

```

## Minority vs Majority
```{r, warning=FALSE, fig.width=10, fig.height=8}
  
all_together$minorities <- as.factor(all_together$minorities)
   
fc_differences_minority <- mm_diffs(all_together, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability  +
                                profile, 
                                id = ~responseid,
                                h0 = 0.0,
                                by = ~minorities,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_minority_plot <- plot(fc_differences_minority, 
                                      vline = 0, 
                                      size = 2, 
                                      feature_headers = FALSE, 
                                      color = "black")

fc_differences_minority_plot_final <- fc_differences_minority_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5,
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_minority, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color = "black", 
                                             size = 3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Minority - majority",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Minority")

```

## Intersectionality
```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_disadv <- all_together %>% mutate(disadvataged =
                  ifelse(higher_education == "Lower educated" & minority == 1 & sex == 2, 
                                        "Excluded", 
                  ifelse(higher_education == "Higher educated" & minority == 2 & sex == 1, 
                                        "Not excluded", "")))
  
all_together_disadv <- all_together_disadv %>% filter(disadvataged=="Excluded" | disadvataged=="Not excluded") 
all_together_disadv$disadvataged <- as.factor(all_together_disadv$disadvataged)
all_together_disadv$disadvataged <- factor(all_together_disadv$disadvataged, 
                                           levels= c("Not excluded",
                                                     "Excluded"))
   
fc_differences_disadv <- mm_diffs(all_together_disadv, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                  profile, 
                                id = ~responseid,
                                h0 = 0,
                                by = ~disadvataged,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile="Profile display"))

fc_differences_disadv_plot <- plot(fc_differences_disadv, 
                                             vline = 0, 
                                             size = 2, 
                                             feature_headers = FALSE, 
                                             color = "black")
        
fc_differences_disadv_plot_final <- fc_differences_disadv_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position="none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size = 15)) +
                                   theme(text = element_text(size = 15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_disadv, 
                                             mapping = aes(label = round(estimate, 3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Excluded - not excluded",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size=12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                    ggtitle("Intersectionality")

```

## Lower vs Higher Educated
```{r, warning=FALSE, fig.width=10, fig.height=8}

all_together_higher_education <- all_together %>% 
    filter(higher_education=="Higher educated" | higher_education=="Lower educated") 
all_together_higher_education$higher_education <- as.factor(all_together_higher_education$higher_education)
   
fc_differences_education <- mm_diffs(all_together_higher_education, 
                                choice ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                profile, 
                                id = ~responseid,
                                h0 = 0.0,
                                by = ~higher_education,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project ="Who is choosing the winning project?",
                                         profile = "Profile display"))

fc_differences_education_plot <- plot(fc_differences_education, 
                                     vline = 0.0, 
                                     size = 2, 
                                     feature_headers = FALSE, 
                                     color = "black")

fc_differences_education_plot_final <- fc_differences_education_plot + 
                                    theme_wsj(base_size = 12, 
                                              color = "gray", 
                                              base_family = "Times New Roman",
                                              title_family = "Times New Roman") + 
                                   theme(legend.position = "none") +
                                   theme(axis.text.y = element_blank()) +
                                   theme(plot.title = element_text(hjust = 0.5, 
                                                                   vjust = 3, 
                                                                   size=15)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, 
                                                       ncol=1, 
                                                       scales = "free_y") +
                                   geom_text(fc_differences_education, 
                                             mapping=aes(label=round(estimate,3)), 
                                             parse = TRUE, 
                                             color="black", 
                                             size=3, 
                                             check_overlap = TRUE, 
                                             vjust = 1.4) +
                                   scale_x_continuous(limits = c(-0.2, 0.2)) +
                                   labs(x = "Lower - higher educated",
                                        y = " ") +
                                   theme(axis.title.x = element_text(size = 12)) +
                                    scale_color_manual(values = c("black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black", 
                                                                  "black")) +
                                  ggtitle("Education")
     
```

## Combine all figures into one
```{r, warning=FALSE, fig.width=20, fig.height=10}
figure_a16 <- ggarrange(fc_differences_gender_plot_final, 
                      fc_differences_education_plot_final,
                      fc_differences_minority_plot_final,
                      fc_differences_disadv_plot_final,
                      ncol = 4, 
                      widths = c(3.1, 1.5, 1.5, 1.5))

plot(figure_a16)

ggsave("figure_a16.png",
       width = 20, 
       height = 10, 
       dpi = 150, 
       units = "in", 
       device='png')

```
